#!/usr/bin/env groovy

node {
    def build_cmd = "pnpm"
    def build_env = ""

    stage('Checkout') {
        echo "0. Checkout stage"

        checkout([
            $class: 'GitSCM',
            branches: [[name: '*/main']],
            userRemoteConfigs: [[
                credentialsId: '5a8f0ac3-4926-4ae7-8790-c3a93e81539e',
                url: 'git@gitee.com:hanshuaishuai/parallels-jenkins.git'
            ]]
        ])
    }
    stage('Test') {
        echo '1. Testing....'
    }
    stage('Buld') {
        echo '2. Build....'

        if (params.BUILD_CMD) build_cmd = params.BUILD_CMD
        if (params.BUILD_ENV) build_env = ":${params.BUILD_ENV}"

        echo "${params.NODE_VERSION}"

        echo "${params.BUILD_CMD}"

        echo "${params.BUILD_ENV}"

        nvm("${params.NODE_VERSION ? params.NODE_VERSION : '24'}") {
            sh """
                node -v
                npm -v

                if [[ "$build_cmd" = "pnpm" ]]; then
                    npm install pnpm -g
                    pnpm -v
                    echo "use pnpm"
                elif [[ "$build_cmd" = "yarn" ]]; then
                    npm install yarn -g
                    yarn -v
                    echo "use yarn"
                else
                    echo "use npm"
                fi

                ${build_cmd} install --registry=https://registry.npmmirror.com/

                if [[ "$build_cmd" != "npm" ]]; then
                    ${build_cmd} build${build_env}
                else
                    ${build_cmd} run build${build_env}
                fi
            """
        }
    }
    stage('Deploy') {
        echo '3. Deploy....'
    }
    stage('Push') {
        echo '4. Push....'
    }
}